package com.hs.controller;

import com.hs.config.MyValues;
import com.hs.config.R;
import com.hs.entity.*;
import com.hs.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 路由
 */
@Controller
public class IndexController {
    @Autowired
    UserService userService;
    @Autowired
    YxClassService yxClassService;
    @Autowired
    NewsService newsService;
    @Autowired
    LessonService lessonService;
    @Autowired
    YxModelService yxModelService;

    /**
     * 首页
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/")
    public String index(HttpSession session,Model model,YxModel record,Lesson lesson){
        Object ob = session.getAttribute("user");


        if(record.getPageSize() == null){
            record.setPageSize(4);
            lesson.setPageSize(4);
        }
        if(record.getCurrentPage() == null ){
            record.setCurrentPage(0);
            lesson.setCurrentPage(0);

        }
        if(record.getFlag() == null){
            record.setFlag(0);
            lesson.setFlag(0);
        }
        record.setCurrentPage(record.getCurrentPage() + record.getFlag());
        record.setPageStart(record.getCurrentPage() *  record.getPageSize());

        lesson.setCurrentPage(record.getCurrentPage() + record.getFlag());
        lesson.setPageStart(record.getCurrentPage() *  record.getPageSize());

        model.addAttribute("page",yxModelService.page(record));
        model.addAttribute("lessonPage",lessonService.page(lesson));
        if(ob ==  null){
            return "index_unLogin";
        }
        return "index";
    }
    @RequestMapping("/moreModel")
    public String moreModel(HttpSession session,Model model,YxModel record){
        if(record.getPageSize() == null){
            record.setPageSize(8);
        }
        if(record.getCurrentPage() == null ){
            record.setCurrentPage(0);
        }
        if(record.getFlag() == null){
            record.setFlag(0);
        }
        record.setCurrentPage(record.getCurrentPage() + record.getFlag());
        record.setPageStart(record.getCurrentPage() *  record.getPageSize());
        model.addAttribute("page",yxModelService.page(record));
        return "common/moreModel";
    }
    @RequestMapping("/moreLesson")
    public String moreLesson(HttpSession session,Model model,Lesson record){
        if(record.getPageSize() == null){
            record.setPageSize(8);
        }
        if(record.getCurrentPage() == null ){
            record.setCurrentPage(0);
        }
        if(record.getFlag() == null){
            record.setFlag(0);
        }
        record.setCurrentPage(record.getCurrentPage() + record.getFlag());
        record.setPageStart(record.getCurrentPage() *  record.getPageSize());
        model.addAttribute("page",lessonService.page(record));
        return "common/moreLesson";
    }
    @RequestMapping("/lessonInfo")
    public String lessonInfo(HttpSession session,Model model,Lesson lesson){

        if(lesson == null || lesson.getId() == null){
            return "error";
        }
        Lesson lesson1 = lessonService.selectInfoByPrimaryKey(lesson.getId());

        session.setAttribute("user",session.getAttribute("user"));
        model.addAttribute("user",session.getAttribute("user"));
        model.addAttribute("lesson",lesson1);

        Object ob = session.getAttribute("user");
        if(ob == null){
            return "lesson_info_unLogin";
        }
        return "common/lesson_info";
    }
    @RequestMapping("/myClass")
    public String myClass(HttpSession session,Model model,YxClass yxClass){
        session.setAttribute("user",session.getAttribute("user"));
        model.addAttribute("user",session.getAttribute("user"));
        model.addAttribute("taskValue",new MyValues());
        if(yxClass.getPageSize() == null){
            yxClass.setPageSize(5);
        }
        if(yxClass.getCurrentPage() == null ){
            yxClass.setCurrentPage(0);
        }
        if(yxClass.getFlag() == null){
            yxClass.setFlag(0);
        }
        yxClass.setCurrentPage(yxClass.getCurrentPage() + yxClass.getFlag());
        yxClass.setPageStart(yxClass.getCurrentPage() *  yxClass.getPageSize());
        model.addAttribute("page",yxClassService.page(yxClass));
        return "teacher/class_list";
    }
    /**
     *  admin  teacher student
     */
    @RequestMapping("/myInfo")
    public String myInfo(HttpSession session,Model model){
        session.setAttribute("user",session.getAttribute("user"));
        model.addAttribute("user",session.getAttribute("user"));
        model.addAttribute("taskValue",new MyValues());
        return "common/my_info";
    }
    @RequestMapping("/news")
    public String news(HttpSession session,Model model,News record){
        Object ob = session.getAttribute("user");
//        if(record.getPageSize() == null){
//            record.setPageSize(3);
//        }
//        if(record.getCurrentPage() == null ){
//            record.setCurrentPage(0);
//        }
//        if(record.getFlag() == null){
//            record.setFlag(0);
//        }
//        record.setCurrentPage(record.getCurrentPage() + record.getFlag());
//        record.setPageStart(record.getCurrentPage() *  record.getPageSize());
//        model.addAttribute("page",newsService.page(record));
        if(ob == null){
            return "news_unLogin";
        }

        return "news";
    }
    @RequestMapping("/about")
    public String about(HttpSession session,Model model){
        Object ob = session.getAttribute("user");
        session.setAttribute("user",session.getAttribute("user"));
        model.addAttribute("user",session.getAttribute("user"));
        model.addAttribute("taskValue",new MyValues());
        if(ob == null){
            return "about_unLogin";
        }

        return "about";
    }
    @RequestMapping("/subject")
    public String subject(HttpSession session,Model model){
        session.setAttribute("user",session.getAttribute("user"));
        model.addAttribute("user",session.getAttribute("user"));
        model.addAttribute("taskValue",new MyValues());
        return "subject";
    }
    @RequestMapping("/modelAdd")
    public String modelAdd(HttpSession session,Model model){
        session.setAttribute("user",session.getAttribute("user"));
        model.addAttribute("user",session.getAttribute("user"));
        return "admin/model_add";
    }
    /**
     * admin -  teacher
     */
    @RequestMapping("/modelList")
    public String model(HttpSession session,Model model,YxModel record){
        if(record.getPageSize() == null){
            record.setPageSize(8);
        }
        if(record.getCurrentPage() == null ){
            record.setCurrentPage(0);
        }
        if(record.getFlag() == null){
            record.setFlag(0);
        }
        record.setCurrentPage(record.getCurrentPage() + record.getFlag());
        record.setPageStart(record.getCurrentPage() *  record.getPageSize());
        model.addAttribute("page",yxModelService.page(record));
        return "common/model_list";
    }
    @RequestMapping("/modelInfo")
    public String modelInfo(HttpSession session,Model model,YxModel yxModel){
        if(yxModel ==  null || yxModel.getId() == null){
            model.addAttribute("msg","id为空啊");
            return "error";
        }
        YxModel yxModel1 = yxModelService.selectByPrimaryKey(yxModel.getId());
        session.setAttribute("user",session.getAttribute("user"));
        model.addAttribute("user",session.getAttribute("user"));
        model.addAttribute("model",yxModel1);

        Object ob = session.getAttribute("user");
        if(ob == null){
            return "model_info_unLogin";
        }
        return "common/model_info";
    }

    @RequestMapping("login")
    public String login(){
        return "login";
    }
    @RequestMapping("loginOut")
    public String loginOut(HttpSession session){
        session.invalidate();
        return "login";
    }

    @RequestMapping("register")
    public String register(){
        return "register";
    }

    @RequestMapping("upload")
    @ResponseBody
    public R upload(@RequestParam("file") MultipartFile file)  {
        // upload/2021-7-16/11112312312312.jpg
        String imgUrl = null;
        if (file.isEmpty()) {
            return R.FAILURE("上传失败",null);
        }
        // 保存的路径的文件,可以根据自己编写路径
//        ClassPathResource classPathResource = new ClassPathResource("upload/");
//        String basePath = classPathResource.getPath();
        Resource resource = new ClassPathResource("");
        String basePath = "";
        try {
            String baseCLassPath = resource.getFile().getAbsolutePath();
            System.out.println(resource.getFile().getAbsolutePath());
            basePath = baseCLassPath + File.separator + "static"+File.separator + "upload";
//            File upload = new File(basePath);
//            if(!upload.exists()){
//                upload.mkdirs();
//            }
        } catch (IOException e) {
            return R.FAILURE(e.getMessage(),e);
        }

        //文件名称
        String OriginalFilename = file.getOriginalFilename();
        String fileName = ""+System.currentTimeMillis()+"."+ OriginalFilename.split("\\.")[1];
        DateFormat bf = new SimpleDateFormat("yyyy-MM-dd");//多态
        Date date = new Date();//创建时间
        String formatDate = bf.format(date);//格式化 bf.format(date);
        // 将文件保存到指定路径的功能
        FileOutputStream out = null;
        try {
            String endPath = basePath + File.separator + formatDate;
            File fileDirendPath = new File(endPath);
            if(!fileDirendPath.exists()){
                fileDirendPath.mkdirs();
            }
            imgUrl = File.separator + "upload"+File.separator + formatDate + File.separator + fileName;
            String filePath = endPath + File.separator + fileName;
            System.out.println(filePath);
            File file1 = new File(filePath);
            if(!file1.exists()){
                file1.createNewFile();
            }

            out = new FileOutputStream(filePath);
            out.write(file.getBytes());
            out.flush();
            out.close();
        } catch (Exception e) {
            return R.FAILURE(e.getMessage(),e);
        }
        return R.SUCCESS("上传成功！",imgUrl);
    }

    public static void main(String[] args) {
        System.out.println("a.jsp".split("\\.")[0]);
    }


}
